<template>
  <div>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { mapMutations } from 'vuex'
export default {
   data() {
    return {
      username: '',
      password: '',
    };
  },
  methods: {
    onSubmit(values) {
      console.log('submit', values);
      setTimeout(() => {
        // 个人信息 和 token
        const userInfo =  {
          nickName: "小花真漂亮",
          avatar: 'https://dss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=448522501,4036185718&fm=15&gp=0.jpg'
        }
        this.setUserInfo(userInfo)
        const access_token = "duwhye87e8dgcye3f"
        localStorage.setItem('access_token', access_token)
      
        Toast({
          message:'登录成功',
          duration:500,
          onClose:()=>{
             if(this.$route.query.from){
              this.$router.push(this.$route.query.from)
            }else{
              this.$router.push('/home')
            }
          }
        })
       
      }, 2000)
    },
    ...mapMutations(['setUserInfo'])
  }
}
</script>

<style lang="scss" scoped>

</style>
